global repfolder = "D:/replication_package_manifesto"
import delimited "$repfolder/data/intermediate/stackedData.csv", clear

replace import = import-1
replace approval = approval-1

forvalues x = 1/257 {
gen promise`x' = 0
forvalues y = 1/4 {
replace promise`x' = 1 if g`y'==`x'
}
}

encode page, gen(pageID)

xtset pageID

xtreg import promise1-promise256, fe
matrix importAll = e(b)'
matrix colnames importAll = importAll

xtreg approval promise1-promise256, fe
matrix approvalAll = e(b)'
matrix colnames approvalAll = approvalAll

xtreg import promise1-promise256 if vote17==1, fe
matrix importCon = e(b)'
matrix colnames importCon = importCon

xtreg approval promise1-promise256 if vote17==1, fe
matrix approvalCon = e(b)'
matrix colnames approvalCon = approvalCon

matrix coeffs = importAll, approvalAll, importCon, approvalCon

clear

svmat coeffs, names(col) 

gen promise = _n

foreach x in importAll approvalAll importCon approvalCon {
egen max = max(`x')
replace `x' = 0 if `x'==max
replace `x' = max +`x'
egen mean = mean(`x')
replace `x' = `x'-mean
drop max mean
}

reshape long import approval, i(promise) j(set) string

egen fixedSD = sd(import)

reshape wide import approval, i(promise) j(set) string

gen wtFixedAll = exp(importAll/fixedSD)
egen total = total(wtFixedAll)
replace wtFixedAll = wtFixedAll/total
drop total

gen wtFixedCon = exp(importCon/fixedSD)
egen total = total(wtFixedCon)
replace wtFixedCon = wtFixedCon/total
drop total

save "$repfolder/data/intermediate/fixed effects model weights.dta", replace

fmm 2: regress importAll

predict prob*, classposteriorpr

keep promise prob2

rename prob2 majorPromiseProb

export delimited using "$repfolder/tables/majorPromiseProb.csv", replace






